﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WC
{
    namespace Utils
    {
        public static class GCEnv
        {
            public static void SetToForceGC(int dueTime, int period)
            {
                if (null == _timer)
                    _timer = new Timer(new TimerCallback(OnTimer), null, dueTime, period);
                else
                    _timer.Change(dueTime, period);
            }
            public static void StopForceGC()
            {
                if (null != _timer)
                    _timer.Change(Timeout.Infinite, Timeout.Infinite);
            }

            private static void OnTimer(Object state)
            {
                long bytes1 = GC.GetTotalMemory(false);
                GC.Collect();
                long bytes2 = GC.GetTotalMemory(false);
                log.WarnFormat("GC: {0:#,##0}KB - {1:#,##0}KB = {2:#,##0}KB", bytes1 / 1024, bytes2 / 1024, (bytes1 - bytes2) / 1024);
            }

            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            private static Timer _timer;
        }
    }
}
